package com.insta.formation.library.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.insta.formation.library.Libraryfactory;
import com.insta.formation.library.Properties;
import com.insta.formation.library.dao.IExemplaryDao;
import com.insta.formation.library.dao.JdbcTools;
import com.insta.formation.library.model.IBook;
import com.insta.formation.library.model.IExemplary;

public class ExemplaryDaoImpl implements IExemplaryDao
{
	final private static Logger LOGGER = LoggerFactory.getLogger(BookDaoImpl.class);

	public boolean AddExemplary(IBook book, int nbr_add) {
				
		try 
		{
			//verifie que le livre demander existe bien en BDD
			String query = "SELECT name FROM " + Properties.BOOK_TABLE + " WHERE isbn="+book.getIsbn();
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

			ResultSet rs = stmt.getGeneratedKeys();
			if (rs.getString(1)!="")
			{
				stmt.clearBatch();
				query = "UPDATE " + Properties.BOOK_TABLE + " SET EXEMPLARY="+nbr_add+"WHERE isbn="+book.getIsbn();
				conn = JdbcTools.INSTANCE.getConnection();
				stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

				boolean rq =stmt.execute() ;
				stmt.close();
				return rq;
			}
		} 
		catch (SQLException e) 
		{
			LOGGER.error(e.getMessage());
		}
		return false;
	}

	public boolean RemoveExemplary(IBook book, int nbr_remove) {
		try 
		{
			//verifie que le livre demander existe bien en BDD
			String query = "SELECT name FROM " + Properties.BOOK_TABLE + " WHERE isbn="+book.getIsbn();
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

			ResultSet rs = stmt.getGeneratedKeys();
			if (rs.getString(1)!="")
			{
				stmt.clearBatch();
				query = "DELETE FROM " + Properties.BOOK_TABLE + " WHERE isbn="+book.getIsbn();
				conn = JdbcTools.INSTANCE.getConnection();
				stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

				boolean rq =stmt.execute() ;
				stmt.close();
				return rq;
			}
		} 
		catch (SQLException e) 
		{
			LOGGER.error(e.getMessage());
		}
		return false;
	}

	public int FindNbrExemplary(IBook book) {
		int result = 0;
		try 
		{
			//verifie que le livre demander existe bien en BDD
			String query = "SELECT name FROM " + Properties.BOOK_TABLE + " WHERE isbn="+book.getIsbn();
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

			ResultSet rs = stmt.getGeneratedKeys();
			if (rs.getString(1)!="")
			{
				stmt.clearBatch();
				query = "SELECT EXEMPLARY FROM " + Properties.BOOK_TABLE + " WHERE isbn="+book.getIsbn();
				conn = JdbcTools.INSTANCE.getConnection();
				stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

				stmt.execute();
				ResultSet rs_find = stmt.getGeneratedKeys();
				if (rs.next()) 
				{
					result = rs.getInt(1);
				}
				return result;
			}
			stmt.close();
		} 
		catch (SQLException e) 
		{
			LOGGER.error(e.getMessage());
		}
		return 0;
	}

	public IExemplary create(IExemplary o) {
		// TODO Auto-generated method stub
		return null;
	}

	public IExemplary find(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public IExemplary update(IExemplary o) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(IExemplary o) {
		// TODO Auto-generated method stub
		
	}

}
